package com.highgain.displacementtrend.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.highgain.displacementtrend.entity.Device;
import com.highgain.displacementtrend.entity.OnLineRateInfo;
import com.highgain.displacementtrend.entity.Result;
import com.highgain.displacementtrend.mapper.DeviceMapper;
import com.highgain.displacementtrend.service.DeviceService;
import com.highgain.displacementtrend.util.PageView;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhanglifeng
 * @date 2021/04/14 11:02
 **/
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {
    @Override
    public Result queryAllDevice(Device device) {
        List<Device> collect = baseMapper.queryAllDevice(device).stream().filter(d -> d.getProvinceCode() != null).collect(Collectors.toList());
        List<String> ids = queryOnlineIds(device);
        collect.stream().forEach(device1 -> {
            if (ids.contains(String.valueOf(device1.getId()))) {
                device1.setOnlineState(1);
            } else {
                device1.setOnlineState(0);
            }
        });
        return Result.ok("").data(collect);
    }

    @Override
    public PageView<Device> queryDevice(PageView<Device> pageView, Device device) {
        List<Device> deviceList = baseMapper.queryAllDevice(device);
        List<String> ids = queryOnlineIds(device);
        deviceList.forEach(device1 -> {
            if (ids.contains(String.valueOf(device1.getId()))) {
                device1.setOnlineState(1);
            } else {
                device1.setOnlineState(0);
            }
        });

        if(device.getOnlineState() != null){
            deviceList = deviceList.stream().filter(device1 -> device1.getOnlineState().equals(device.getOnlineState())).collect(Collectors.toList());
        }

        int begin = (pageView.getPageNow() - 1) * pageView.getPageSize();
        if(begin > deviceList.size()){
            begin = 0;
        }
        int size = begin + pageView.getPageSize();
        if(size > deviceList.size()){
            size = deviceList.size();
        }
        pageView.setRowCount(((int) deviceList.size()));
        pageView.setRecords(deviceList.subList(begin, size));
        return pageView;
    }

    @Override
    public OnLineRateInfo queryOnlineRate(Device device) {
        OnLineRateInfo onLineRateInfo = new OnLineRateInfo();
        int allCount = baseMapper.getAllDeviceCountByCode(device);
        int online = baseMapper.getOnlineCountByCode(device);
        double onlineRate = online * 1.00 / allCount;
        onLineRateInfo.setDeviceTotal(allCount);
        onLineRateInfo.setOnlineDeviceCount(online);
        onLineRateInfo.setOfflineDeviceCount(allCount - online);
        onLineRateInfo.setOnlineRate(String.format("%.2f", onlineRate * 100) + "%");
        return onLineRateInfo;
    }

    @Override
    public List<String> queryOnlineIds(Device device){
        return baseMapper.getOnlineDeviceId(device);
    }

    @Override
    public String queryHiddenPointName(int hiddentPointId) {
        return baseMapper.queryHiddenPointName(hiddentPointId);
    }


}
